<?php

/**
 * oRs
 *
 * @version 3.0
 * @copyright 2009 Ron Planken
 * @class: downloads
 * @description: Handles all download calls
 */

class downloads {

	private $_system;
	
	function __construct($system) {
		$this->_system = $system;
	}

	function get($download_id = -1) {

		if ($download_id == -1) {
			$query ='SELECT d.download_id
							,d.file_name
							,d.file_size
							,d.name
							,d.description
							,d.screenshot_name
							,d.user_level
					   FROM downloads d
					  ORDER by d.download_id ASC
							';
		} else {
			$download_id = $this->_system->_db->secure_int($download_id);
			
			$query ="SELECT d.download_id
							,d.file_name
							,d.file_size
							,d.name
							,d.description
							,d.screenshot_name
							,d.user_level
					   FROM downloads d
					  WHERE d.download_id = $download_id
					  ORDER by d.download_id ASC
							";
	}

		$downloads = $this->_system->_db->selectMultiQuery($query);

		return $downloads;
	}
	
	function save($download_id = 0, $file_name = "", $file_size = "0", $name = "", $description = "", $screenshot_name = "", $user_level = 0) {

		$download_id = $this->_system->_db->secure_int($download_id);
		$file_name = $this->_system->_db->secure_string($file_name);
		$file_size = $this->_system->_db->secure_string($file_size);
		$name = $this->_system->_db->secure_string($name);
		$description = $this->_system->_db->secure_string($description);
		$screenshot_name = $this->_system->_db->secure_string($screenshot_name);
		$user_level = $this->_system->_db->secure_int($user_level);

		if ($download_id == 0) {
			$query ="INSERT INTO downloads
						   (download_id
							,file_name
							,file_size
							,name
							,description
							,screenshot_name
							,user_level)
					VALUES ( $download_id
							,$file_name
							,$file_size
							,$name
							,$description
							,$screenshot_name
							,$user_level)
							";
			$status = $this->_system->_db->InsertQuery($query);

		} else {
			$query ="UPDATE downloads
						SET file_name = $file_name
							,file_size = $file_size
							,name = $name
							,description = $description
							,screenshot_name = $screenshot_name
							,user_level = $user_level
					  WHERE download_id = $download_id
							";
			
			$status =  $this->_system->_db->UpdateQuery($query);
		}

		return $status;

	}

	function remove($download_id = 0) {

		$download_id =  $this->_system->_db->secure_int($download_id);
		
		$query ="DELETE FROM downloads
				  	   WHERE download_id = $download_id;
							";
		$status =  $this->_system->_db->UpdateQuery($query);

		return $status;
	}
	
}
?>